<?php

namespace Home\Controller;
use Think\AjaxPage;
use Think\Page;
class SystemController extends BaseController{
        /**
         * 清空系统缓存
         */
        public function cleanCache(){              
             delFile('./Application/Runtime');// 清除所有                 
            // 删除静态文件                
            $html_arr = glob("./Application/Runtime/Html/*.html");
            foreach ($html_arr as $key => $val)
            {
               unlink($val);                                     
            }                               
            $this->success("操作完成!!!");
        }
	    
      
    
     
     function ajax_get_action()
     {
     	$control = I('controller');
     	$advContrl = get_class_methods("Home\\Controller\\".$control);
     	//dump($advContrl);
     	$baseContrl = get_class_methods('Home\Controller\BaseController');
     	$diffArray  = array_diff($advContrl,$baseContrl);
     	$html = '';
     	foreach ($diffArray as $val){
     		$html .= "<option value='".$val."'>".$val."</option>";
     	}
     	exit($html);
     }
     
     /**
      * 权限资源列表
      */
     function right_list(){
     	/* $group = array('system'=>'系统设置','plan'=>'计划管理','quality'=>'质量管理','cost'=>'成本管理'
     	);
     	$keywords = I('keywords');
     	$grou = I('group');
     	if(empty($keywords) || empty($grou)){
     	    $right_list = M('system_menu')->select();
     	}else{
     	    $right_list = M()->query("select * from system_menu where name like '%$keywords%' AND group like '%$grou%' order by id");
     	}
     	$this->assign('right_list',$right_list);
     	$this->assign('group',$group); */
     	$this->display();	
     }
     
     /**
      * ajax权限分页
      */
     function ajaxright(){
         $group = array('system'=>'系统设置','plan'=>'计划管理','quality'=>'质量管理','cost'=>'成本管理','project'=>'项目管理','bim'=>'BIM管理','family' => '族库管理'
         );
         // 搜索条件
         $condition = array();
         $count =   M('system_menu')->count();
         $Page  = new AjaxPage($count,10);
         //  搜索条件下 分页赋值
         foreach($condition as $key=>$val) {
             $Page->parameter[$key]   =   urlencode($val);
         }
         $right_list = M('system_menu')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
         $show = $Page->show();
         $this->assign('right_list',$right_list);
         $this->assign('page',$show);// 赋值分页输出
         $this->assign('group',$group);
         $this->display();
     }
     
     /**
      * 管理资源列表
      */
     public function edit_right(){
     	if(IS_POST){
     		$data = I('post.');
     		$data['right'] = implode(',',$data['right']);
     		if(!empty($data['id'])){
     			M('system_menu')->where(array('id'=>$data['id']))->save($data);
     			adminLog('编辑权限资源',__ACTION__);
     		}else{
     			if(M('system_menu')->where(array('name'=>$data['name']))->count()>0){
     				$this->error('该权限名称已添加，请检查',U('System/right_list'));
     			}
     			unset($data['id']);
     			M('system_menu')->add($data);
     			adminLog('添加权限资源',__ACTION__);
     		}
     		$this->success('操作成功',U('System/right_list'));
     		exit;
     	}
     	$id = I('id');
     	if($id){
     		$info = M('system_menu')->where(array('id'=>$id))->find();
     		$info['right'] = explode(',', $info['right']);
     		$this->assign('info',$info);
     	}
     	$group = array('system'=>'系统设置','plan'=>'计划管理','quality'=>'质量管理','cost'=>'成本管理','project'=>'项目管理','bim'=>'BIM管理','family' => '族库管理'
     	);
     	$planPath = APP_PATH.'Home/Controller';
     	$planList = array();
     	$dirRes   = opendir($planPath);
     	while($dir = readdir($dirRes))
     	{
     		if(!in_array($dir,array('.','..','.svn')))
     		{
     			$planList[] = basename($dir,'.class.php');
     		}
     	}
     	$this->assign('planList',$planList);
     	$this->assign('group',$group);
     	$this->display();
     }
     
     /**
      * 删除权限资源
      */
     public function right_del(){
     	$id = I('del_id');
     	if(is_array($id)){
     		$id = implode(',', $id); 
     	}
     	if(!empty($id)){
     		$r = M('system_menu')->where("id in ($id)")->delete();
     		if($r){
     		    adminLog('删除权限资源',__ACTION__);
     			respose(1);
     		}else{
     			respose('删除失败');
     		}
     	}else{
     		respose('参数有误');
     	}
     }
     
     /**
      * 流程定义
      */
     public function definition(){
         $pid = session('pid');
         $Modal = M('process');
         
         $da['pid'] = $pid;
         $da['add_time'] = time();
         
         if(IS_POST){
             $da['name'] = I('post.name');
             if(!is_numeric($pid)){
                 exit(json_encode(array('errNo'=>1,'msg'=>'当前所处项目为空,所以无法添加流程名')));
             }
             $d = $Modal->where(array('name'=>$da['name'],'pid'=>$pid))->count();
             if($d){
                 exit(json_encode(array('errNo'=>1,'msg'=>'流程名称已存在')));
             }
             $res = $Modal->add($da);
             if($res){
                 exit(json_encode(array('errNo'=>0,'msg'=>'添加成功')));
             }else{
                 exit(json_encode(array('errNo'=>1,'msg'=>'添加失败,请重试')));
             }
         }
         if($pid){
             $r = $Modal->where('pid='.$pid)->select();
         }
             foreach ($r as $key=>$val){
                 $type[] = $val['type'];
             }
             if(!in_array(1,$type) && is_numeric($pid)){
                 $da['type'] = 1;
                 $da['name'] = '编制报告';
                 $Modal->add($da);
             }
       /*      if(!in_array(2,$type) && is_numeric($pid)){
                 $da['type'] = 2;
                 $da['name'] = '下发任务';
                 $Modal->add($da);
             }  */
         if($pid){
            $data = $Modal->where(array('pid'=>$pid))->select();
         }
         $library = $Modal->where('pid=0 AND type=2')->find();
         if($library['launch'] !=''){
             $library = 2;
             $this->assign('library',$library);
         }
         
         $this->assign('data',$data);
         $this->display();
     }
     
     /**
      * COPY抄送人
      */
     public function ajaxdefinition(){
         $Modal = M('process');
         $id = I('post.id');
         if(is_numeric($id)){
             $setpm = $Modal->field('copy')->where('id='.$id)->find();
             $setpm = explode(',',$setpm['copy']);
             if($setpm){
                 $this->assign('setpm',$setpm);
             }
         }
         $sc = new \Home\Controller\ProjectController();
         $company = $sc->member();
         $this->assign(array(
             'company'=>$company,
             'id' => $id
         ));
         $this->display();
     }
     /**
      * 保存抄送人
      */
     public function savecopy(){
         $data = I('post.');
         if($data){
             if(!$data['id']){
                 exit(json_encode(array('errNo'=>1,'msg'=>'ID有误')));
             }
             $a = $data['copy'];
             $joi = array_unique($a);
             $copy = implode(',',$joi);
             $res = M('process')->where(array('id'=>$data['id']))->save(array('copy'=>$copy,'add_time'=>time()));
             if($res){
                 exit(json_encode(array('errNo'=>0,'msg'=>'设置成功')));
             }else{
                 exit(json_encode(array('errNo'=>1,'msg'=>'设置失败')));
             }
         }
     }
     
     /**
      *审批人页
      */
     public function approval(){
         $id = I('get.id');
         $typeid = I('get.type');
         if(!$id){
             $this->error('id不能为空');
         }
         if(is_numeric($typeid)){
             $ename = M('examine_name')->where(array('typeid'=>$typeid))->select();
         }
         if($id == 'all' && $typeid ==2){
             $pid = 0;
         }else{
             $pid = session('pid');
         }
         $user = A('project')->member();
         $setmp = M('process_data')->field('examine_id,eid')->where(array('pid'=>$pid,'typeid'=>$typeid))->select();
         foreach ($user as $key=>$val){
             foreach ($val['user'] as $kk=>$vv){
                 foreach ($setmp as $k=>$v){
                     if($vv['admin_id']==$v['examine_id']){
                         $user[$key]['user'][$kk]['eid'] = $v['eid'];
                     }
                 }
             }
         }
         $se = M('process')->field('launch')->where(array('pid'=>$pid,'type'=>$typeid))->find();
         $se = explode(',',$se['launch']);
         $this->assign('se',$se);
         $this->assign('user',$user);
         $this->assign('setmp',$setmp);
         $this->assign('ename',$ename);
         $this->assign('typeid',$typeid);
         $this->assign('id',$id);
         $this->display();
     }
     
    /**
     * 保存审批人员
     */
     public function saveapproval(){
         $examine = I('post.examine'); 
         $id = session('pid');
         $typeid = I('post.typeid');
         if($typeid==2){
             $id = 0;
         }
         $laun = I('post.launch'); 
         if(!is_numeric($id) ||!is_numeric($typeid)){
             exit(json_encode(array('errNo'=>1,'msg'=>'ID不能为空,请刷新重试')));
         }
         if(empty($examine) || empty($laun)){
             exit(json_encode(array('errNo'=>1,'msg'=>'数据不能为空,请刷新重试')));
         }
         $Modal = M('process_data');
         $r = explode('.',$examine[0]);
         $rr = $Modal->where(array('eid'=>$r[1],'pid'=>$id,'typeid'=>$typeid))->find();
         if(empty($rr)){
             foreach($examine as $key){
                 $examine = explode('.',$key);
                 $res = $Modal->data(array('pid' => $id, 'eid' => $examine[1], 'examine_id' => $examine[0],'typeid'=>$typeid))->add();
             }
         }else{
             foreach($examine as $key){
                 $examine = explode('.',$key);
                 $res = $Modal->where(array('eid'=>$examine[1],'pid'=>$id,'typeid'=>$typeid))->save(array('examine_id' => $examine[0]));
             }
         }
         $launch = implode(',',$laun);
         $res = M('process')->where(array('pid'=>$id,'type'=>$typeid))->save(array('launch'=>$launch));
         if($res !== false){
             exit(json_encode(array('errNo'=>0,'msg'=>'保存设置成功')));
         }else{
             exit(json_encode(array('errNo'=>1,'msg'=>'保存设置失败')));
         }
     }
}